<template>
    <ul class="list">
        <li class="item" 
        v-for="item of letters" 
        :key="item"
        :ref="item" 
        @click="handleLetterClick"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
        >{{item}}</li>
    </ul>
</template>

<script>
export default {
    name : 'CityAlphabet',
    props : {
        cities : Object
    },
    data() {
        return {
            touchStatus : false,
            StartY : 0,
            timer : null
        }
    },
    computed: {
        letters () {
            const letter = []
            for (let i in this.cities){
                letter.push(i)
            }
            return letter
        }
    },
    updated() {
        this.StartY=this.$refs['A'][0].offsetTop
    },
    methods: {
        handleLetterClick (e) {
            // console.log(e.target.innerHTML)
            this.$emit ("change" ,e.target.innerHTML)
        },
        handleTouchStart () {
            this.touchStatus = true
        },
        handleTouchMove (e) {
            if(this.timer){
                clearTimeout(this.timer)
            }
            this.timer=setTimeout(()=>{
               if (this.touchStatus){
                    const TouchY = e.touches[0].clientY-84
                    const index = Math.floor((TouchY-this.StartY)/this.$refs['A'][0].offsetHeight)
               if( index>=0&&index<this.letters.length){
                    this.$emit('change',this.letters[index])
               }
            }
            },16)
            
        },
        handleTouchEnd () {
            this.touchStatus = false
        }
    },
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';
    .list
        position : absolute
        top : 1.62rem
        right : 0
        bottom : 0
        display : flex
        width : .4rem
        flex-direction : column
        justify-content : center
        .item
            text-align : center
            line-height : .44rem
            color : $bgColor
</style>

